﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_CompetePriceDEC : System.Web.UI.Page
{
   int CDECProjectId;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            CDECProjectId = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["projectid"]));
            string _strPassWord = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["PassWord"]));
            //if (!HZ.MBSM.BLL.Login.CheckPerson("jf001", _strPassWord, "4"))
            //{
            //    Response.Write("<script>window.alert('对不起,密码不对！');</script>");
            //    Response.Write("<script>window.location=\"systemlogo.aspx?Projectid=" + CDECProjectId.ToString() + "&Id=2\"</script>");
            //}
            //else
            //{
                
                string sql;
                string count1 = "";
                string count2 = "";
                Db db = new Db();
                System.Data.IDataReader dr;
                sql = " select a.cun as cun1,b.cun as cun2 from (select count(*) as cun From ProjectCompanyAttribution where ProjectCompanyAttribution.Memo1=1 and projectid=" + CDECProjectId.ToString() + " and ProjectCompanyAttribution.accountid in (Select distinct accountid From Project_Bid,CompeteTable where ProjectId= " + CDECProjectId.ToString() + " And Project_Bid.bidid=CompeteTable.bidid)) a," +
                      " (select count(*) as cun from ProjectCompanyAttribution where (ProjectCompanyAttribution.Memo1=0  or ProjectCompanyAttribution.Memo1 is null) And projectid=" + CDECProjectId.ToString() + " and ProjectCompanyAttribution.accountid in (select distinct accountid from Project_Bid,CompeteTable Where projectid= " + CDECProjectId.ToString() + " And Project_Bid.bidid=CompeteTable.bidid)) b";//memo1解密状态
                dr = db.DataReader(sql);
                while (dr.Read())
                {
                    count1 = dr["cun1"].ToString();
                    count2 = dr["cun2"].ToString();
                }
                dr.Close();
                dr.Dispose();
                sql = "select count(*) as cun from ProjectCompanyAttribution where memo3=1 and  projectid=" + CDECProjectId.ToString();//memo3解密开关
                dr = db.DataReader(sql);
                while (dr.Read())
                {
                    if (SysFun.ToInt(dr["cun"]) > 0)
                    {
                        labJM.Text = "解密已开起";
                    }
                    else
                    {
                        labJM.Text = "解密已关闭";
                    }

                }
                dr.Close();
                dr.Dispose();
                db.Close();
                TextBox1.Text = count1;
                TextBox2.Text = count2;
                showGridView();
            //}
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[3].Text == "1")
            {
                if (e.Row.Cells[6].Text == "1")
                {
                    e.Row.Cells[2].Text = "<a  style='background-color:Yellow' href=PrintPriceCompete.aspx?accountid=" + e.Row.Cells[4].Text + "&projectid=" + CDECProjectId + "&accountname=" + e.Row.Cells[0].Text + " target='_blank'>已解密</a>";
                    e.Row.Cells[5].Text = "<a  style='background-color:Yellow' href=PrintPriceCompete1.aspx?accountid=" + e.Row.Cells[4].Text + "&projectid=" + CDECProjectId + "&accountname=" + e.Row.Cells[0].Text + " target='_blank'>打印</a>";
                }
                else
                {
                    e.Row.Cells[2].Text = "<a  href=PrintPriceCompete.aspx?accountid=" + e.Row.Cells[4].Text + "&projectid=" + CDECProjectId + "&accountname=" + e.Row.Cells[0].Text + " target='_blank'>已解密</a>";
                    e.Row.Cells[5].Text = "<a  href=PrintPriceCompete1.aspx?accountid=" + e.Row.Cells[4].Text + "&projectid=" + CDECProjectId + "&accountname=" + e.Row.Cells[0].Text + " target='_blank'>打印</a>";
                }
            }
            else
            {
                e.Row.Cells[2].Text = "未解密";
                e.Row.Cells[5].Text = "";
            }

        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[3].Visible = false;
        e.Row.Cells[4].Visible = false;
        e.Row.Cells[6].Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Db db = new Db();       
        string strSql = "update ProjectCompanyAttribution set memo3=1 where projectid=" + SysFun.ToTrim (Request.QueryString["projectid"]);
        db.Command(strSql);
        db.Close();
        labJM.Text = "解密已开起";
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", strSql, "../Agency/CompetePriceDEC.aspx");


    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Db db = new Db();
        string strSql = "update ProjectCompanyAttribution set memo3=0 where projectid=" + SysFun.ToTrim(Request.QueryString["projectid"]);
        db.Command(strSql);
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", strSql, "../Agency/CompetePriceDEC.aspx");

        string _strTable = "JinJiaJL" + CDECProjectId.ToString();
        strSql = "if exists (select 1  from  sysobjects  where  id = object_id('" + _strTable + "')  and   type = 'U')  drop table " + _strTable;
        db.Command(strSql);
        strSql = "select ProjectId,BidPrice.BidId,GoodsId, ProductCode, BidTableName, BidTableType, BidTableOutlookc,ProductId, ProductName,SpellingCode,Medicinemodel, Outlookc, Unit,GoodsName, Factor, Pack,SCNAME as CompanyName_SC, AccountId_SC,FactoryUserCode as UserCode_SC,PackUnit, QualityLevels,  CompeteMinPrice, CompetePackPrice, PricePassword, RetinuePassword, AccountID_TB,TBRNAME as CompanyName_TB,TBUserCode as UserCode_TB,Convert(Varchar(255),'') as BeiZhu into " + _strTable + " from bidprice,CompeteTable where Memo1=1 and CheckStatus=1 and status=1 and bidprice.bidid=CompeteTable.bidid and projectid=" + CDECProjectId.ToString();
        db.Command(strSql);
        db.Close();
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "生成竞价开标表", "JinJiaJL", strSql, "../Agency/CompetePriceDEC.aspx");

        Button3.Visible = true;
        labJM.Text = "解密已关闭";
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {
        string strConditions;
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "ProjectCompanyAttribution,company";
        dbPage.Key = "ProjectCompanyAttribution.accountid";
        dbPage.Where = strConditions;                        //
        dbPage.SelectField = "ProjectCompanyAttribution.accountid,companyname,ProjectCompanyAttribution.Memo1,company.usercode,ProjectCompanyAttribution.memo5";                             //可以不设，默认为“*”
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 30;//Request.QueryString["PS"]);
        dbPage.OrderBy = "ProjectCompanyAttribution.Memo1 desc,ProjectCompanyAttribution.CompeteBidDate,ProjectCompanyAttribution.accountid";
        
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        db.Close();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        String SelDEC = DDLBidSuccess.SelectedValue.ToString();
        String strtemp="";
        switch (SelDEC)
        {
            case "全部":
                break;
            case "已解密成功":
                strtemp += "  ProjectCompanyAttribution.Memo1='1' and";
                break;
            case "未解密":
                strtemp += "  ProjectCompanyAttribution.Memo1='0' and";
                break;
        }
        //投标人注册号
        String UserCode_TB = "";
        UserCode_TB = TextBox3.Text.Trim();
        if (UserCode_TB != "")
        {
            strtemp += " UserCode Like '%" + UserCode_TB + "%' And";
        }
        //投标人名称
        string _strCompanyName = txtCompanyName_TB.Text.ToString().Trim();
        if (_strCompanyName != "")
        {
            strtemp += " companyname Like '%" + _strCompanyName + "%' And";
        }
        strtemp += " ProjectCompanyAttribution.accountid=company.accountid and ProjectCompanyAttribution.accountid in (select distinct accountid From Project_Bid,CompeteTable where projectid= " + CDECProjectId.ToString() + " And Project_Bid.bidid=CompeteTable.bidid) and" +
                         " ProjectCompanyAttribution.projectid=" + CDECProjectId.ToString() + " And";
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        String Sql;
        Sql = "Update Project_Bid Set CheckStatus='3' Where Accountid in (Select Accountid From ProjectCompanyAttribution Where Memo1=0 And ProjectId="+ CDECProjectId.ToString() +") And CheckStatus=1 And Status=1 And ProjectId="+ CDECProjectId.ToString();
        Db db = new Db();
        db.Command(Sql);
        db.Close();
        Response.Write("<script>window.alert('废标成功！');</script>");
    }
}
